package com.cuckoo.mall.thus.menuaccount;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.BaseFragment;
import com.bgn.baseframe.network.HttpObserver;
import com.bgn.baseframe.network.bean.TDataBean;
import com.bgn.baseframe.utils.imageloader.GlideCircleTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.UserData;
import com.cuckoo.mall.network.ApiManager;
import com.cuckoo.mall.thus.manager.UserInfoManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by wanglun on 2018/3/19.
 */

public class AccountFragment extends BaseFragment {


    @BindView(R.id.tv_setting)
    TextView tvSetting;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_my_order)
    TextView tvMyOrder;
    @BindView(R.id.tv_my_save)
    TextView tvMySave;
    @BindView(R.id.tv_my_viewd)
    TextView tvMyViewd;
    @BindView(R.id.tv_my_ticket)
    TextView tvMyTicket;

    @BindView(R.id.tv_about_us)
    TextView tvAboutUs;
    @BindView(R.id.lly_my_service)
    LinearLayout llyMyService;
    @BindView(R.id.lly_facebook)
    LinearLayout llyFacebook;
    @BindView(R.id.rly_login)
    RelativeLayout rlyLogin;
    @BindView(R.id.lly_unLogin)
    LinearLayout llyUnLogin;

    @Override
    protected View initView(LayoutInflater inflater) {
        View rootView = inflater.inflate(R.layout.fragment_menu_account, null);
        ButterKnife.bind(this, rootView);
        // EventBus.getDefault().register(this);
        return rootView;
    }

    @Override
    public void onDestroy() {
        // EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        resetUi();
    }


    public void resetUi() {
        if (UserInfoManager.getInstance().isLogin()) {
            rlyLogin.setVisibility(View.VISIBLE);
            llyUnLogin.setVisibility(View.GONE);
            if (UserInfoManager.getInstance().getUserInfo().getData() != null && UserInfoManager.getInstance().isLoadUserDataSucceed()) {
                if (TextUtils.isEmpty(UserInfoManager.getInstance().getUserInfo().getData().getNick_name())) {
                    tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getEmail());
                } else {
                    tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getNick_name());
                }
                Glide.with(getActivity()).load(UserInfoManager.getInstance().getUserInfo().getData().getHead_img()).apply(new RequestOptions().priority(Priority.IMMEDIATE).transform(new GlideCircleTransform(getActivity())).placeholder(R.mipmap.head).error(R.mipmap.head)).into(ivHead);
            } else {
                showLoadingView();
                ApiManager.getUserData().subscribe(new HttpObserver<TDataBean<UserData>>(getBaseActivity()) {
                    @Override
                    public void onSucceed(TDataBean<UserData> value) {
                        UserInfoManager.getInstance().updateUserData(value.getData());
                        if (TextUtils.isEmpty(UserInfoManager.getInstance().getUserInfo().getData().getNick_name())) {
                            tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getEmail());
                        } else {
                            tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getNick_name());
                        }
                        Glide.with(getActivity()).load(UserInfoManager.getInstance().getUserInfo().getData().getHead_img()).apply(new RequestOptions().priority(Priority.IMMEDIATE).transform(new GlideCircleTransform(getActivity())).placeholder(R.mipmap.head).error(R.mipmap.head)).into(ivHead);
                    }

                });
            }
        } else {
            rlyLogin.setVisibility(View.GONE);
            llyUnLogin.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.tv_my_ticket, R.id.lly_facebook, R.id.tv_setting, R.id.iv_head, R.id.tv_name, R.id.tv_my_order, R.id.tv_my_save, R.id.tv_my_viewd, R.id.tv_about_us})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_my_ticket:
                if (UserInfoManager.getInstance().checkTrunLogin()) {
                    ARouter.getInstance().build("/mall/ticketListActivity").navigation();
                }
                break;
            case R.id.iv_head:
            case R.id.tv_name:
            case R.id.tv_setting:
                if (UserInfoManager.getInstance().checkTrunLogin() && UserInfoManager.getInstance().getUserInfo().getData() != null) {
                    ARouter.getInstance().build("/mall/settingActivity").navigation();
                }
                break;
            case R.id.tv_my_order:
                if (UserInfoManager.getInstance().checkTrunLogin()) {
                    ARouter.getInstance().build("/mall/orderlistActivity").navigation();
                }
                break;
            case R.id.tv_my_save:
                if (UserInfoManager.getInstance().checkTrunLogin()) {
                    ARouter.getInstance().build("/mall/favoriteActivity").navigation();
                }
                break;
            case R.id.tv_my_viewd:
                if (UserInfoManager.getInstance().checkTrunLogin()) {
                    ARouter.getInstance().build("/mall/recentlyGoodsListActivity").navigation();
                }
                break;

            case R.id.tv_about_us:
                ARouter.getInstance().build("/mall/aboutUsActivity").navigation();
                break;
            case R.id.lly_facebook:
                // showLoadingView();
                //doAuthorizeLogin(Facebook.NAME);

                ARouter.getInstance().build("/mall/loginActivity").navigation();
                break;

        }
    }


//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onMessageEvent(MessageEvent event) {
//        hideLoadingView();
//        switch (event.getEvenCode()) {
//            case "onComplete":
//                if (event.getDataString().equals("FACE_BOOK")) {
//                    HashMap<String, Object> data = (HashMap<String, Object>) event.getData();
//                    String facebook_id = (String) data.get("id");
//                    String email = (String) data.get("email");
//                    String phone = (String) data.get("phone");
//                    String third_party_id = (String) data.get("third_party_id");
//                    String nickname = (String) data.get("name");
//                    String head_img = event.getPlatform().getDb().getUserIcon();
//                    final String aseKey = ASEUtils.getKey();
//                    ApiManager.thridPartLogin(facebook_id, email, phone, third_party_id, nickname, head_img, aseKey).subscribe(new HttpObserver<TDataBean<UserInfo>>(this) {
//                        @Override
//                        public void onSucceed(TDataBean<UserInfo> value) {
//                            String encodeToken = value.getData().getToken();
//                            String token = ASEUtils.Decrypt(encodeToken, aseKey);
//                            value.getData().setToken(token);
//                            if (value.getData().getRealtoken().equals(token)) {
//                                ToastUtil.showInfo(getString(R.string.login_succeed));
//                                UserInfoManager.getInstance().loginSucceed(value.getData());
//                                resetUi();
//                            }
//                        }
//
//                        @Override
//                        public void onDefeat(TDataBean<UserInfo> value) {
//                            super.onDefeat(value);
//
//                        }
//
//                        @Override
//                        public void onError(Throwable e) {
//                            super.onError(e);
//                        }
//                    });
//                }
//                break;
//            case "onError":
//                Throwable error = (Throwable) event.getData();
//                ToastUtil.showInfo(error.getMessage());
//                break;
//            case "onCancel":
//                ToastUtil.showInfo(getString(R.string.cancel));
//                break;
//        }
//    }

}
